Ext.onReady(function() {
    // Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
    var psize = 18;
    var store;
    var jReader1;
    var relate_grid; 
    var nodeid;
    //id=218
    
     var openUrl = basePath + 'JarpListener?className=cn.com.jsepc.rep.report.GetRepPara';
             openUrl += '&methodName=get_para_report&paramCount=9';
    jReader1 = new Ext.data.JsonReader( {
        root: 'data',
        totalProperty: 'totalCount',
        fields : [ {
            name : 'repid'
        }, {
            name : 'repname'
        }, {
            name : 'repcode'
        }, {
            name : 'cataid'
        }, {
            name : 'repdesc'
        }, {
            name : 'desc'
        }, {
            name : 'dataprec'
        }, {
            name : 'dataunit'
        }, {
            name : 'department'
        }, {
            name : 'departmentid'
        }, {
            name : 'sourcetype'
        }, {
            name : 'datatype'
        }, {
            name : 'updatetime'
        }, {
            name : 'reffact'
        }, {
            name : 'repvaluecol'
        },{
            name: 'isselect'
        } ]
    });
		
    store = new Ext.data.Store( {
        storeId : 'store1',
        proxy : new Ext.data.HttpProxy( {
            url : openUrl
        }),
        reader : jReader1,
        baseParams : {
            param_0:0,
            param_1:psize,
            param_2:"",
            param_3:"",
            param_4:"",
            param_5:"",
            param_6:"",
            param_7:"",
            param_8:id
                
        }
    });
   
    var sm = new Ext.grid.CheckboxSelectionModel({
        checkOnly:true
    // singleSelect : true
    });

    var cm = new Ext.grid.ColumnModel( [ sm, {
        hidden:true,
        header : '指标编号',
        sortable : true,
        dataIndex : 'repid'
    }, {
        header : '指标名称',
        sortable : true,
        dataIndex : 'repname'
    }, {
        header : '指标编码',
        sortable : true,
        dataIndex : 'repcode'
    }, {
        header : '所属分类',
        sortable : true,
        dataIndex : 'cataid'
    }, {
        hidden:true,
        header : '指标含义',
        sortable : true,
        dataIndex : 'repdesc'
    }, {
        hidden:true,
        header : '算法描述',
        sortable : true,
        dataIndex : 'desc'
    }, {
        hidden:true,
        header : '数据精度',
        sortable : true,
        dataIndex : 'dataprec'
    }, {
        hidden:true,
        header : '数据单位',
        sortable : true,
        dataIndex : 'dataunit'
    }, {
        header : '归口部门',
        sortable : true,
        dataIndex : 'department'
    }, {
        hidden:true,
        header : '归口部门ID',
        sortable : true,
        dataIndex : 'departmentid'
    }, {
        header : '数据来源类型',
        sortable : true,
        dataIndex : 'sourcetype',
        renderer : function(v){
            if(v==1) {
                return "自动抽取 ";
            }else if(v==2) {
                return "手工填报";
            }
        }
    }, {
        hidden:true,
        header : '数据口径',
        sortable : true,
        dataIndex : 'datatype'
    }, {
        hidden:true,
        header : '更新周期',
        sortable : true,
        dataIndex : 'updatetime'
    }, {
        hidden:true,
        header : '关联事实表',
        sortable : true,
        dataIndex : 'reffact'
    }, {
        hidden:true,
        header : '指标值字段',
        sortable : true,
        dataIndex : 'repvaluecol'
    },{   
        hidden:true,
        dataIndex:'isselect',
        renderer:function(value,cellmeta,record,rowIndex){
            if(value!="" && value!="0"){
                sm.selectRow(rowIndex,true);
            }
        }
    }   
    ]);

    relate_grid = new Ext.grid.GridPanel( {
        id : 'rolegrid',
          anchor: '100% 78%',
        store : store,
        viewConfig:{
            forceFit: true
        },
        sm : sm,
        cm : cm,
        frame : true,
        columnLines : true,
        loadMask:true,
        height:screen.height-400,
        stateful : true,
        stateId : 'grid',
        bbar: new Ext.PagingToolbar({
            store: store,
            paramNames : {
                start : 'param_0',
                limit : 'param_1'
            }, 
            emptyMsg : '没有数据需要显示',
            displayInfo: true,
            pageSize: psize,
            listeners:{
                beforechange:function(bar,params){
                    bar.store.setBaseParam("param_2",Ext.ComponentMgr.get("mingcheng").getValue());
                    bar.store.setBaseParam("param_3",Ext.ComponentMgr.get("bumen").getValue());
                    bar.store.setBaseParam("param_4",Ext.ComponentMgr.get("laiyuan").getValue());
                    bar.store.setBaseParam("param_5",Ext.ComponentMgr.get("koujing").getValue());
                    bar.store.setBaseParam("param_6",Ext.ComponentMgr.get("zhouqi").getValue());
                    bar.store.setBaseParam("param_7",nodeid);
                    bar.store.setBaseParam("param_8",id);
                }
            }
        })
    });
   // relate_grid.render('grid_role');
    store.load();
		
    var toolbar = new Ext.Toolbar( {
        //   id : 'toolbar',
        stateful : true,
        items : ['-', {
            text : '保存',
            handler : function() {
                saveSubject_pool();
            }
        },'-',{
            text:'关闭',
             handler : function() {
                 window.close();
            }      
          }
        ]
    });
    //			dept_cx_ begin
    var selectedNode_dept_cx = null;
    var comb_dept_cx = new Ext.form.ComboBox(
    {
        id : 'dept_cx',
        store : new Ext.data.SimpleStore( {
            fields : [],
            data : [ [] ]
        }),
        editable : false,
        fieldLabel : '归口部门',
        shadow : false,
        mode : 'local',
        emptyText : '请选择归口部门',
        anchor : '100%',
        triggerAction : 'all',
        tpl : '<tpl for="."><div style="height:200px"><div id="tree_dept_cx"></div></div></tpl>',
        selectedClass : '',
        onSelect : Ext.emptyFn
    });
    comb_dept_cx.on("collapse", function() {

        });
			
    var tree_root_dept_cx = new Ext.tree.AsyncTreeNode( {
        text : '部门',
        id : -1
    });

    var tree_dept_cx = new Ext.tree.TreePanel(
    {
        border : '0',
        height : 200,
        useArrows : true,
        autoScroll : true,
        animate : true,
        enableDD : false,
        containerScroll : true,
        rootVisible : false,
        root : tree_root_dept_cx,
        autoScroll : true,
        lines : false,
        iconCls : 'nav',
        checkModel : 'cascade',
        onlyLeafCheckable : false,
        titleCollapse : true,
        listeners : {
            beforeload : function(node) {
                var openUrl_cx = basePath + 'JarpListener?className=cn.com.jsepc.rep.report.CorpDeptTree&methodName=getCorpDeptTreeData&paramCount=2';
                openUrl_cx += '&param_0='+node.id+'&param_1=';
                node.loader = new Ext.tree.TreeLoader( {
                    dataUrl : openUrl_cx
                });
            }
        }
    });


    tree_dept_cx.on('click', function(node) {
        selectedNode_dept_cx = node;
        var nodename = node.text;
        Ext.ComponentMgr.get("dept_cx").setValue(nodename);
        Ext.ComponentMgr.get("bumen").setValue(node.id);		
        selectedNode_dept_cx = null;
        comb_dept_cx.canCollapse = true; 
        comb_dept_cx.collapse();
    });

    comb_dept_cx.on('expand', function() {
        tree_dept_cx.render('tree_dept_cx');
    });

    tree_dept_cx.on('expandnode', function() { comb_dept_cx.canCollapse = true; });
    tree_dept_cx.on('beforeload', function() { comb_dept_cx.canCollapse = false; });
    tree_dept_cx.on('beforeexpandnode', function() { comb_dept_cx.canCollapse = false; });
    tree_dept_cx.on('beforecollapsenode', function() { comb_dept_cx.canCollapse = false; });

     comb_dept_cx.on('expand', function () {
            comb_dept_cx.canCollapse = true;
            if (tree_dept_cx) {
                comb_dept_cx.canCollapse = true;          
                tree_dept_cx.getRootNode().expand();
                
            }
      });
     comb_dept_cx.on('collapse', function() {
            if (!comb_dept_cx.canCollapse) {
                comb_dept_cx.expand();
            }
        });  
				
    //			dept_cx end
    var viewport = new Ext.Viewport( {
        layout : 'border',
        items : [ {
            region : 'center',
            frame : true,
            layout : 'border',
            tbar : toolbar,
            items : [ {
                region : 'west',
                //     id : 'west-panel',
               // title : '指标分类',
               // frame : true,
                width : 140,
                minSize : 200,
                maxSize : 200,
                // collapsible : true,
                html : '<div id="tree_role"></div>'
            }, 
            new Ext.Panel( {
                region : 'center',
                frame : true,
                title : '指标列表',
                deferredRender : false,
                activeTab : 0,
                frame : true,
                 layout:"anchor",
                // collapsible : true,
                // form : 'fit',
              //  html : '<div id="grid_role"></div>',     
                items: [new Ext.FormPanel({
                    //      id : 'p_query',
                    layout : 'form',
                     anchor: '-0,-0',
                    labelWidth : 80,
                    width : '98%',
                    height: '100%',
                    bodyStyle : 'padding: 0 10px 0 10px',
                    border : false,
                    items : [ {
                        title : '查询条件',
                        layout : 'form',
                        xtype : 'fieldset',
                        bodyStyle : 'padding: 0 0 0 0px',
                        collapsible : true,
                        collapsed : false,
                        labelAlign : 'right',
                        height : '100%',
                        defaults : {
                            width : '100%'
                        },
                        items : [
                        {
                            layout : 'column',
                            border : false,
                            items : [ {
                                columnWidth : .3,
                                layout : 'form',
                                labelWidth : 80,
                                border : false,
                                bodyStyle : 'padding:5px 0 0',
                                items : [
                                {
                                    xtype : "textfield",
                                    fieldLabel : '指标名称',
                                    emptyText : '请填写',
                                    width : '100%',
                                    id : 'mingcheng'
                                } ]
                            },{
                                columnWidth : .4,
                                layout : 'form',
                                labelWidth : 80,
                                border : false,
                                bodyStyle : 'padding:5px 0 0',
                                items : [
                                comb_dept_cx,{
                                    id : 'bumen',
                                    fieldLabel : '归口部门',
                                    xtype : "hidden",
                                    emptyText : '请填写',
                                    // emptyText:'',
                                    width : '100%'
                                } ]
                            } ]
                        },
                        {
                            layout : 'column',
                            border : false,
                            items : [ 
                           {
                                xtype:'hidden',
                                id:'koujing'
                            },

                            {
                                columnWidth : .3,
                                layout : 'form',
                                labelWidth : 80,
                                border : false,
                                bodyStyle : 'padding:5px 5px 0',
                                items : [
                                {
                                    xtype : "combo",
                                    triggerAction : "all",
                                    fieldLabel : "数据来源",
                                    anchor : '100%',
                                    emptyText : '请选择',
                                    id : 'laiyuan',
                                    editable : false,
                                    store : [ [ '1', '自动抽取' ],
                                    [ '2', '手工填报' ] ,	[ '3', '全部']]
                                } ]
                            },{
                                columnWidth : .4,
                                layout : 'form',
                                labelWidth : 80,
                                anchor : '80%',
                                width : '80%',
                                border : false,
                                bodyStyle : 'padding:5px 0 0',
                                items : [{
                                    xtype : "combo",
                                    triggerAction : "all",
                                    fieldLabel : "更新周期",
                                    emptyText : '请选择',
                                    blankText : '此项必选',
                                    id : 'zhouqi',
                                    anchor : '100%',
                                    editable : false,
                                    store : [ [ '1', '年' ],
                                    [ '2', '季' ], [ '3', '月' ],
                                    [ '4', '日' ], [ '5', '时' ],
                                    [ '6', '分' ],['7','全部'] ]
                                }]
                            },{
                                columnWidth : .3,
                                layout : 'form',
                                labelWidth : 80,
                                anchor : '100%',
                                border : false,
                                bodyStyle : 'padding:5px 0 0 40px',
                                items : [{
                                    xtype : "button",
                                    //     id : 'query_tbar',
                                    text : "查    询",
                                    width : 60,
                                    handler : function() {
                                        query();
                                    }
                                } ]
                            }
                            ]
                        } 
                        ]
                    } ]	    	

                }),relate_grid]
                
                
            }) ]
        } ]
    });
	
		
    var tree_root = new Ext.tree.AsyncTreeNode( {
        text : '指标分类',
        id :'0'
    });
    var tree = new Ext.tree.TreePanel(
    {
        id:'terr_fenlei',
        tools:[{
            id:'refresh',
            handler:function(){
                tree_root.reload();
            }
        }],
         frame:true,
         title:'指标分类',
        renderTo : 'tree_role',
        border : '0',
        height : 900,
       // width : 200,
        useArrows : true, // 使用箭头
        autoScroll : true,
        animate : true, // 动画
        enableDD : false, // 可拖动
        containerScroll : true,
        rootVisible : true,
        root : tree_root,
        autoScroll : true,
        listeners : {
            beforeload : function(node) {
                var openUrl = basePath + 'JarpListener?className=cn.com.jsepc.rep.catalog.CatalogDAO&methodName=getCatalog&paramCount=0'
                node.loader = new Ext.tree.TreeLoader( {
                    dataUrl : openUrl
                });
            }
        }
    });
		
		
   
		
    tree.on('click',function(node) {
        nodeid = node.id;
        nodeid  = node.id;
        store.load({
            params : {
                param_2:Ext.ComponentMgr.get("mingcheng").getValue(),
                param_3:Ext.ComponentMgr.get("bumen").getValue(),
                param_4:Ext.ComponentMgr.get("laiyuan").getValue(),
                param_5:Ext.ComponentMgr.get("koujing").getValue(),
                param_6:Ext.ComponentMgr.get("zhouqi").getValue(),
                param_7:nodeid,
                param_8:id
            }
        });	
        setempty();
    });
		
		
    function query() {
	
        var keyword_2 = Ext.ComponentMgr.get("mingcheng").getValue();
        var keyword_3 = Ext.ComponentMgr.get("bumen").getValue();
        var keyword_4 = Ext.ComponentMgr.get("laiyuan").getValue();
        var keyword_5 = Ext.ComponentMgr.get("koujing").getValue();
        var keyword_6= Ext.ComponentMgr.get("zhouqi").getValue();	

        store.load({
            params : {
                param_2:keyword_2,
                param_3:keyword_3,
                param_4:keyword_4,
                param_5:keyword_5,
                param_6:keyword_6,
                param_7:nodeid,
                param_8:id
            }
        });	
    //   setempty();
    }
    
    
      
    //保存
    function saveSubject_pool(){
        var grids=store.getCount();
        var array1=new Array(grids);
        for(var i=0;i<grids;i++)
        {
            array1[i]=store.getAt(i).get('repid');
        // array1[i]=grids[i].data.get("repid");
        // alert(array1[i]);
        }
               
        var row = relate_grid.getSelectionModel().getSelections(); 
      //  alert("rows:"+row.data.repid);
        if(row.length==0){
            alert("请勾选需要保存的指标！")
            return ;
        }
        var array=new Array(row.length);
        for(var i=0;i<row.length;i++){
            array[i]=row[i].get('repid');    
        }
         var saveItemInfoUrl = "";
        saveItemInfoUrl = '?className=cn.com.jsepc.rep.report.GetRepPara';
        saveItemInfoUrl += '&methodName=updata_rep_para';
        saveItemInfoUrl += '&paramCount=2'+'&param_0='+ array+ '&param_1=' + id;
        saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
        httpR_itemInfo = getXMLHttpRequest();
        httpR_itemInfo.onreadystatechange = returnItemUpdate;
        httpR_itemInfo.open("POST", saveItemInfoUrl, true);
        httpR_itemInfo.send();
    }
    function returnItemUpdate() {
        if (httpR_itemInfo.readyState == 4) {
            if (httpR_itemInfo.status == 200) { 
                ret = httpR_itemInfo.responseText;
                if (ret=='ok'){
                    Ext.Msg.alert('提示','保存成功！');
                    
                }else{
                    Ext.Msg.alert('提示','保存失败！');
                }
            }
        }
    }

		
    function setempty() {
        Ext.ComponentMgr.get("mingcheng").setValue("");
        Ext.ComponentMgr.get("bumen").setValue("");
        Ext.ComponentMgr.get("laiyuan").setValue("");
        Ext.ComponentMgr.get("koujing").setValue("");
        Ext.ComponentMgr.get("zhouqi").setValue("");
    };
});